package com.kingyea.mobilepolice.apimanage.utils.page;

import com.github.pagehelper.PageHelper;
import com.kingyea.mobilepolice.apimanage.utils.NumberUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 分页器,在Mapper执行前执行 PageInfo.startPage(...) 方法即可
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description pao
 * @date Created in 2017/11/14 19:06
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    //当前页
    private int pageNum = 1;
    //每页的数量
    private int pageSize = 10;

    //总记录数
    private long total;

    //结果集
    private List<T> list;


    public PageInfo() {
    }

    /**
     * 开始分页
     *
     * @param map
     * @return void
     * @author Mr.Lin
     * @date 2017/11/20 16:01
     */
    public static void startPage(Map<String, Object> map) throws Exception {
        Integer pageNum = NumberUtils.obj2Int(map.get("pageNum"));
        Integer pageSize = NumberUtils.obj2Int(map.get("pageSize"));
        startPage(pageNum, pageSize);
    }

    /**
     * 开始分页
     *
     * @param pageNum
     * @param pageSize
     * @return void
     * @author Mr.Lin
     * @date 2017/11/20 16:03
     */
    public static void startPage(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(NumberUtils.isNullOrLEZero(pageNum) ? 1 : pageNum, NumberUtils.isNullNumObj(pageSize) ? 10 : pageSize);
    }

    /**
     * 获取分页对象
     *
     * @param list
     * @return com.kingyea.app.market.utils.page.PageInfo
     * @author Mr.Lin
     * @date 2017/11/14 19:15
     */
    public static <I> PageInfo getPageInfo(List<I> list) {
        PageInfo pageInfo = new PageInfo();
        com.github.pagehelper.PageInfo p = new com.github.pagehelper.PageInfo(list);
        pageInfo.setList(list);
        pageInfo.setPageNum(p.getPageNum() == 0 ? 1 : p.getPageNum());
        pageInfo.setPageSize(p.getPageSize());
        pageInfo.setTotal(p.getTotal());
        return pageInfo;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}
